﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

using WlanDesign;
using Autodesk.AutoCAD.Interop;

namespace WlanDesignDll
{
    public partial class FormAttenuationSet : Form
    {
        
        

        public FormAttenuationSet()
        {
            InitializeComponent();
        }

        private void FormAttenuationSet_Load(object sender, EventArgs e)
        {
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";

            string penet0 = ReadIniTxt(iniName, "attenuation", "penetrance_attenuation");//读取原ini文件中所有的用户名组成的字符串
            string refl0 = ReadIniTxt(iniName, "attenuation", "reflection_attenuation");
            this.tBox_Penet.Text = penet0;
            this.tBox_Ref.Text = refl0;   
      
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
             Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
             Database acCurDb = acDoc.Database;
            if (this.tBox_Penet.Text == "")
            {
                MessageBox.Show("请输入透射衰减度！");
                this.tBox_Penet.Focus();
                return;
            }
            else if (this.tBox_Ref.Text == "")
            {
                MessageBox.Show("请输入反射衰减度！");
                this.tBox_Ref.Focus();
                return;
            }
            string penet = this.tBox_Penet.Text;
            string refl = this.tBox_Ref.Text;

            // ini文件与当前文档在同一目录下，并且名字相同
            //string[] name = acDoc.Name.Split('.');
            //string iniName = name[0] + ".ini";
            string[] name1 = acDoc.Name.Split('.');
            string iniName1 = name1[0] + "子.ini";
            // 写入ini文件
            WriteIniTxt(iniName1, "attenuation", "penetrance_attenuation", penet);
            WriteIniTxt(iniName1, "attenuation", "reflection_attenuation", refl);

            //acDoc.SendStringToExecute("AttenuationSet", true, false, false);
            Autodesk.AutoCAD.Interop.AcadApplication acAppComObj = null;
            const string strProgId = "AutoCAD.Application.18";
            // Get a running instance of AutoCAD
            try
            {
                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                }
                catch
                {
                    // If an instance of AutoCAD is not created then message and exit
                    System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                    return;
                }
            }
            AcadDocument acDocComObj;

            acDocComObj = acAppComObj.ActiveDocument;
            acDocComObj.SendCommand("AttenuationSet ");
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";
            // 写入ini文件
            WriteIniTxt(iniName, "attenuation", "penetrance_attenuation", penet);
            WriteIniTxt(iniName, "attenuation", "reflection_attenuation", refl);
            MessageBox.Show("保存成功！");
            //this.MinimizeBox = true;
            // this.WindowState = FormWindowState.Minimized;
            this.Close();
        }

        private void btn_Undo_Click(object sender, EventArgs e)
        {
            this.tBox_Penet.Clear();
            this.tBox_Ref.Clear();
        }

        //
        // Summery
        // 加载信息到ini文件
        //
        public void WriteIniTxt(string sPath, string Section, string Key, string value)
        {
            string ServerName = value;

            IniFileRW ini = new IniFileRW(sPath);

            ini.IniWriteValue(Section, Key, ServerName);
        }
        //
        // Summery
        // 从ini文件读取信息
        //
        public static string ReadIniTxt(string sPath, string Section, string Key)
        {
            string ServerName;
            IniFileRW ini = new IniFileRW(sPath);
            ServerName = ini.IniReadValue(Section, Key);
            string strSql = ServerName;
            return strSql;
        }

        private void btn_Dif_Click(object sender, EventArgs e)
        {
            //this.Enabled = false;
            //this.Visible = false;
            //this.WindowState = FormWindowState.Minimized;
            MessageBox.Show("请选择对象！");
            /*
            //acDoc.SendStringToExecute("AttenuationSetSub2", true, false, false);
            Autodesk.AutoCAD.Interop.AcadApplication acAppComObj = null;
            const string strProgId = "AutoCAD.Application.18";
            // Get a running instance of AutoCAD
            try
            {
                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                }
                catch
                {
                    // If an instance of AutoCAD is not created then message and exit
                    System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                    return;
                }
            }
            AcadDocument acDocComObj;

            acDocComObj = acAppComObj.ActiveDocument;
            acDocComObj.SendCommand("AttenuationSetSub2 ");
             */
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            // ini文件与当前文档在同一目录下，并且名字相同
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";

            string biuldLyr = ReadIniTxt(iniName, "layer", "building_layer");// 建筑物图层

            Entity acEnt = null;
            bool flag = true;
            while(flag)
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表记录   Open the Block table record for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                PromptEntityOptions peo = new PromptEntityOptions("\n请选择对象：");
                PromptEntityResult per = acDoc.Editor.GetEntity(peo);
                if (per.Status != PromptStatus.OK) return;


                //PromptEntityResult p = acDoc.Editor.GetEntity("请选择对象：");
                //if (p == null)
                //{ return; }
                acEnt = acTrans.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
                // 判断选择的是否为建筑物
                if (acEnt.Layer == biuldLyr) flag = false;
                else { flag = true; MessageBox.Show("您选择的对象不是建筑物，请重新选择！"); }
            }
            string[] name1 = acDoc.Name.Split('.');
            string iniName1 = name1[0] + "子.ini";
            // 写入ini文件

            WriteIniTxt(iniName1, "handle", "handlevalue", acEnt.Handle.Value.ToString());
            
            
                FormAttenuationSetSub1 dif = new FormAttenuationSetSub1();
                //this.Close();
                dif.Visible = true;

            
            
            //this.Enabled = true;
            //this.Close();
        }
    }
}
